package cn.mark.test.serlvet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.mark.test.serlvet.handler.XYZPUpgradeHandler;

@WebServlet(urlPatterns={"/xyzpresource"})
public class XYZPUpgradeServlet extends HttpServlet {
	@Override
	   public void doGet(HttpServletRequest request,
	                     HttpServletResponse response) throws IOException {
	      if ("XYZP".equals(request.getHeader("Upgrade"))) {
	         /* Accept upgrade request */
	         response.setStatus(101);
	         response.setHeader("Upgrade", "XYZP");
	         response.setHeader("Connection", "Upgrade");
	         response.setHeader("OtherHeaderB", "Value");
	         /* Delegate the connection to the upgrade handler */
	         try {
				XYZPUpgradeHandler handler= request.upgrade(XYZPUpgradeHandler.class);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	         /* (the service method returns immedately) */
	      } else {
	         response.getWriter().write("not upgrade");
	      }
	      
	      response.getWriter().write("end");
	   }
}
